package cn.edu.scau.cmi.chenjia.spring.dao;

import java.util.List;
import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.edu.scau.cmi.chenjia.spring.domain.teacher;
import cn.edu.scau.cmi.chenjia.hibernate.util.ScauCmiHibernateSessionFactoryUtil;



public class TeacherDAO{
	private static final Logger log = LoggerFactory.getLogger(TeacherDAO.class);
	// property constants
	public static final String NAME = "name";
	private Session session = ScauCmiHibernateSessionFactoryUtil.getSession();

	public void save(teacher transientInstance) {
		log.debug("saving Teacher instance");
		try {
			Transaction transaction = session.beginTransaction();
			session.save(transientInstance);
			transaction.commit();
			
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}


	public void delete(teacher persistentInstance) {
		log.debug("deleting Teacher instance");
		try {
			session.delete(persistentInstance);
			Transaction transaction = session.beginTransaction();
			transaction.commit();
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public teacher findById(java.lang.Long id) {
		log.debug("getting Teacher instance with id: " + id);
		try {
			teacher instance = (teacher) session.get("cn.edu.scau.cmi.chenjia.hibernate.domain.Teacher", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(teacher instance) {
		log.debug("finding Teacher instance by example");
		try {
			List results = session.createCriteria("cn.edu.scau.cmi.chenjia.hibernate.domain.Teacher")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: " + results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Teacher instance with property: " + propertyName + ", value: " + value);
		try {
			String queryString = "from Teacher as model where model." + propertyName + "= ?";
			Query queryObject = session.createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List findAll() {
		log.debug("finding all Teacher instances");
		try {
			String queryString = "from Teacher";
			Query queryObject = session.createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public teacher merge(teacher detachedInstance) {
		log.debug("merging Teacher instance");
		try {
			teacher result = (teacher) session.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(teacher instance) {
		log.debug("attaching dirty Teacher instance");
		try {
			Transaction transaction = session.beginTransaction();
			session.update(instance);
			transaction.commit();
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(teacher instance) {
		log.debug("attaching clean Teacher instance");
		try {
			session.buildLockRequest(LockOptions.NONE).lock(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}
